package cate.game.play.param;


import cate.common.table.d.GD;
import cate.game.play.fighter.HeroFighter;
import cate.game.play.part.FightTeam;
import cp.solution.util.collection.MapKit;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@SuppressWarnings("unchecked")
@NoteClass("血量继承")
public class HpExtend  {

    @NoteField("根据英雄uid记录的血量百分比")
    private Map<String, Double> uidRates;

    @NoteField("根据英雄tid记录的血量百分比")
    private Map<Integer, Double> tidRates;


    public Map<String, Double> getUidRates() {
        return uidRates;
    }

    public Map<Integer, Double> getTidRates() {
        return tidRates;
    }

    public void read(HpExtend other) {
        if (other == null) {
            return;
        }
        this.uidRates = other.uidRates;
        this.tidRates = other.tidRates;
    }

    /**
     * 记录单个英雄的血量
     * @param uid
     * @param hpCurRate
     */
    public void recordByHeroUid(String uid, double hpCurRate) {
        if (uidRates == null) {
            uidRates = new HashMap<>();
        }
        uidRates.put(uid, hpCurRate);
    }

    /**
     * 记录单个英雄的血量
     * @param tid
     * @param hpCurRate
     */
    public void recordByHeroTid(int tid, double hpCurRate) {
        if (tidRates == null) {
            tidRates = new HashMap<>();
        }
        tidRates.put(tid, hpCurRate);
    }

    /**
     * 根据uid记录血量
     * @param ft
     */
    public void recordByUid(FightTeam ft) {
        if (uidRates == null) {
            uidRates = new HashMap<>();
        }
        ft.getAllHero().forEach(f -> uidRates.put(f.uid, f.attr.total().hpCurRate() / GD.W_10000_0));
    }

    /**
     * 根据英雄id记录血量
     * @param ft
     */
    public void recordByTid(FightTeam ft) {
        if (tidRates == null) {
            tidRates = new HashMap<>();
        }
        ft.getAllHero().forEach(f -> tidRates.put(f.heroTid, f.attr.total().hpCurRate() / GD.W_10000_0));
    }

    /**
     * 清除血量继承
     */
    public void clear(){
        uidRates = null;
        tidRates = null;
    }


    /**
     * 战斗开始前移除血量为0的单位
     * @param ft
     */
    public void beforeFight(FightTeam ft) {
        Iterator<HeroFighter> iter = ft.getAllHero().iterator();
        while (iter.hasNext()) {
            HeroFighter f = iter.next();
            Double rate = getRate(f);
            if (rate != null && rate <= 0) {
                iter.remove();
            }
        }
    }


    /**
     * 战斗开始时 血量赋值（已经算好阵容羁绊属性 死亡的单位已经被移除）
     * @param ft
     */
    public void onFightBegin(FightTeam ft) {
        for (HeroFighter f : ft.getAllHero()) {
            Double rate = getRate(f);
            if (rate != null && rate > 0) {
                rate = Math.min(1, rate);
                f.attr.hpCur(Math.ceil(f.attr.total().hpTotal() * rate));
            } else {
                f.attr.hpCur(f.attr.total().hpTotal());
            }
        }
    }

    private Double getRate(HeroFighter f) {
        if (uidRates != null) {
            return uidRates.get(f.uid);
        }
        if (tidRates != null) {
            return tidRates.get(f.heroTid);
        }
        return null;
    }


    public HpExtend copy() {
        HpExtend cp = new HpExtend();
        cp.uidRates = MapKit.clone(this.uidRates);
        cp.tidRates = MapKit.clone(this.tidRates);
        return cp;
    }
}
